package com.otoe.dilidilivid.view.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.otoe.dilidilivid.R;
import com.otoe.dilidilivid.data.dao.VideoDao;
import com.otoe.dilidilivid.data.entity.VideoInfo;
import com.otoe.dilidilivid.adapter.VideoListAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class CollectionVideoActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    List<VideoInfo> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collection_video);

        List<VideoInfo> datas = initData();

        @SuppressLint("HandlerLeak")
        final Handler handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                list=(ArrayList<VideoInfo>)msg.obj;
                List<VideoInfo> datas =new ArrayList<>();
                int videoCover[]={R.drawable.video1,R.drawable.video2,R.drawable.video3,R.drawable.video4,R.drawable.video5, R.drawable.video6,R.drawable.video7,R.drawable.video8,R.drawable.video9,R.drawable.video10,R.drawable.video11, R.drawable.video12,R.drawable.video13,R.drawable.video14,R.drawable.video15,R.drawable.video16,R.drawable.video17, R.drawable.video18,R.drawable.video19,R.drawable.video20};
                int i=0;
  /*              for(VideoInfo videoInfo:list){
                    videoInfo.setImgUrl(videoCover[i]);
                    i++;
                }*/
                Random random=new Random();
                for(int j=0;j< random.nextInt(10);j++){
                    datas.add(list.get(random.nextInt(20)));
                }
                recyclerView = (RecyclerView) findViewById(R.id.rv_show);
                recyclerView.setLayoutManager(new LinearLayoutManager(CollectionVideoActivity.this,LinearLayoutManager.VERTICAL,false));
                recyclerView.setAdapter(new VideoListAdapter(CollectionVideoActivity.this,datas));
            }
        };
        new Thread(){
            @Override
            public void run(){
                VideoDao videoDao=new VideoDao();
                //videoDao.changeDuration();
                Message msg=Message.obtain();
                msg.obj=videoDao.selectAll();
                handler.sendMessage(msg);
            }
        }.start();
    }
    List<VideoInfo> initData(){
        List<VideoInfo> mdata = new ArrayList<VideoInfo>();
        int i,videoCover[]={R.drawable.video1,R.drawable.video2,R.drawable.video3,R.drawable.video4,R.drawable.video5, R.drawable.video6,R.drawable.video7,R.drawable.video8,R.drawable.video9,R.drawable.video10,R.drawable.video11, R.drawable.video12,R.drawable.video13,R.drawable.video14,R.drawable.video15,R.drawable.video16,R.drawable.video17, R.drawable.video18,R.drawable.video19,R.drawable.video20};
        for(int n=0;n<7;n++){
            i=(int)(Math.random()*20);
            VideoInfo videoInfo = new VideoInfo();
            videoInfo.title="Video"+i;
            videoInfo.uname ="UP"+i;
            videoInfo.playNums =(int)(Math.random()*1000);
            videoInfo.duration=(int)(Math.random()*10)+":"+(int)(Math.random()*60);
            videoInfo.imgUrl =videoCover[i];
            mdata.add(videoInfo);
        }
        return mdata;
    }
}